﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using KeRui.Model;
using System.Text;
using System.Collections.Generic;

public partial class Manage_Cms_CatchSingleParseItem : System.Web.UI.Page
{
    protected int ParseID = PageRequest.GetInt("pcid", -1);
    protected string act = PageRequest.GetString("act");

    /// <summary>
    /// 内容规则
    /// </summary>
    protected KeRui.BLL.CmsCatchParseContent BLLCmsCatchParseContent = new KeRui.BLL.CmsCatchParseContent();
    /// <summary>
    /// 内容补充
    /// </summary>
    protected KeRui.BLL.CmsCatchParseRegexContent BLLRegexContent = new KeRui.BLL.CmsCatchParseRegexContent();
    protected List<KeRui.Model.CmsCatchParseRegexContent> listRegex = new List<CmsCatchParseRegexContent>();
    /// <summary>
    /// 内容规则
    /// </summary>
    protected KeRui.Model.CmsCatchParseContent ModelCmsCatchParseContent = new CmsCatchParseContent();
    /// <summary>
    /// 内容补充
    /// </summary>
    protected KeRui.Model.CmsCatchParseRegexContent ModelCmsCatchParseRegexContent = new CmsCatchParseRegexContent();

    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";


    protected void Page_Load(object sender, EventArgs e)
    {
        //登录
        ManageHelper.CheckAdminLogin();
        //权限
        //ManageHelper.CheckAdminPower("system_catchparsemanage");
        switch (act)
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;

            case "editSave":
                EditSave();
                break;

            //----集解析内容调整
            case "regadd":
                RegAdd();
                break;
            case "regaddsave":
                RegAddSave();
                break;
            case "regedit":
                RegEdit();
                break;
            case "regeditsave":
                RegEditSave();
                break;
            case "regDel":
                RegDel();
                break;
            default:
                break;
        }
    }



    protected void Add()
    {
        //ManageHelper.CheckAdminPower("system_catchparesmanageadd");
        ModelCmsCatchParseContent.ParseContentName = PageRequest.GetString("ParseContentName");
        if (ModelCmsCatchParseContent.ParseContentName == "")
        {
            nsb.AddErr("请输入名称。");
        }
        ModelCmsCatchParseContent.IsUsing = PageRequest.GetInt("IsUsing", 0);
        ModelCmsCatchParseContent.ContentEncoding = PageRequest.GetString("ContentEncoding");
        if (ModelCmsCatchParseContent.ContentEncoding == "")
        {
            ModelCmsCatchParseContent.ContentEncoding = "GB2312";
        }
        ModelCmsCatchParseContent.ContentParseBegin = PageRequest.GetString("ContentParseBegin");
        if (ModelCmsCatchParseContent.ContentParseBegin == "")
        {
            nsb.AddErr("请输入定位开头。");
        }
        ModelCmsCatchParseContent.ContentParseEnd = PageRequest.GetString("ContentParseEnd");
        if (ModelCmsCatchParseContent.ContentParseEnd == "")
        {
            nsb.AddErr("请输入定位结尾。");
        }
        ModelCmsCatchParseContent.ContentParseTags = PageRequest.GetString("ContentParseTags");
        if (ModelCmsCatchParseContent.ContentParseTags == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        ModelCmsCatchParseContent.ContentParseAttrs = PageRequest.GetString("ContentParseAttrs");
        if (ModelCmsCatchParseContent.ContentParseAttrs == "")
        {
            nsb.AddErr("请输入格式化XHTML属性。");
        }
        ModelCmsCatchParseContent.ContentParseRule = PageRequest.GetString("ContentParseRule");
        if (ModelCmsCatchParseContent.ContentParseRule == "")
        {
            nsb.AddErr("请输入内容解析规则。");
        }
        ModelCmsCatchParseContent.ContentPageParseRule = PageRequest.GetString("ContentPageParseRule");


        //admin信息
        ModelCmsCatchParseContent.CreateTime = System.DateTime.Now;
        ModelCmsCatchParseContent.CreateUserID = 10000;
        ModelCmsCatchParseContent.CreateUserName = "admin";
        ModelCmsCatchParseContent.UpdateTime = System.DateTime.Now;
        ModelCmsCatchParseContent.UpdateUserID = 10000;
        ModelCmsCatchParseContent.UpdateUserName = "admin";

        ModelCmsCatchParseContent.BlackList = "";
        ModelCmsCatchParseContent.WhileList = "";

        nsb.ChkErr();
        int RetFlag = BLLCmsCatchParseContent.Add(ModelCmsCatchParseContent);
        if (RetFlag < 0)
        {
            nsb.AddErr("对不起！已存在相同的内容模型。");
            nsb.ChkErr();
        }
        else
        {
            //ManageHelper.AddLog("system_catchparesmanageupdate", "修改了采集解析：" + ModelCmsCatchParseContent.ParseContentName);
            Response.Redirect("CatchSingleParseList.aspx?sucmsg=添加成功。");
        }

    }

    protected void Edit()
    {
        ActFlag = "editSave";
        ActName = "修改";
        if (ParseID <= 0)
        {
            nsb.AddErr("非法操作。");
            nsb.ChkErr();
        }
        ModelCmsCatchParseContent = BLLCmsCatchParseContent.GetModel(ParseID);
        if (ModelCmsCatchParseContent == null)
        {
            nsb.AddErr("该采集解析不存在。");
            nsb.ChkErr();
        }
    }

    private void Del()
    {
        //ManageHelper.CheckAdminPower("system_catchparesmanagedelete");

        if (ParseID <= 0)
        {
            nsb.AddErr("该采集规则不存在。");
            nsb.ChkErr();
        }
        BLLCmsCatchParseContent.Delete(ParseID);
        //ManageHelper.AddLog("system_catchparesmanagedelete", "删除了采集解析，ID：" + ParseID);
        Response.Redirect("CatchSingleParseList.aspx?sucmsg=恭喜。删除成功。");
    }

    protected void EditSave()
    {
        //ManageHelper.CheckAdminPower("system_catchparesmanageupdate");
        ModelCmsCatchParseContent =BLLCmsCatchParseContent.GetModel(ParseID);
        if (ModelCmsCatchParseContent==null)
        {
            ModelCmsCatchParseContent.ParseContentID = ParseID;
            ModelCmsCatchParseContent.CreateTime = System.DateTime.Now;
            ModelCmsCatchParseContent.CreateUserID = 10000;
            ModelCmsCatchParseContent.CreateUserName = "admin";
            ModelCmsCatchParseContent.BlackList = "";
            ModelCmsCatchParseContent.WhileList = "";
        }
        ModelCmsCatchParseContent.ParseContentName = PageRequest.GetString("ParseContentName");
        if (ModelCmsCatchParseContent.ParseContentName == "")
        {
            nsb.AddErr("请输入名称。");
        }
        ModelCmsCatchParseContent.IsUsing = PageRequest.GetInt("IsUsing", 0);
        ModelCmsCatchParseContent.ContentEncoding = PageRequest.GetString("ContentEncoding");
        if (ModelCmsCatchParseContent.ContentEncoding == "")
        {
            ModelCmsCatchParseContent.ContentEncoding = "GB2312";
        }
        ModelCmsCatchParseContent.ContentParseBegin = PageRequest.GetString("ContentParseBegin");
        if (ModelCmsCatchParseContent.ContentParseBegin == "")
        {
            nsb.AddErr("请输入定位开头。");
        }
        ModelCmsCatchParseContent.ContentParseEnd = PageRequest.GetString("ContentParseEnd");
        if (ModelCmsCatchParseContent.ContentParseEnd == "")
        {
            nsb.AddErr("请输入定位结尾。");
        }
        ModelCmsCatchParseContent.ContentParseTags = PageRequest.GetString("ContentParseTags");
        if (ModelCmsCatchParseContent.ContentParseTags == "")
        {
            nsb.AddErr("请输入格式化XHTML标签。");
        }
        ModelCmsCatchParseContent.ContentParseAttrs = PageRequest.GetString("ContentParseAttrs");
        if (ModelCmsCatchParseContent.ContentParseAttrs == "")
        {
            nsb.AddErr("请输入格式化XHTML属性。");
        }
        ModelCmsCatchParseContent.ContentParseRule = PageRequest.GetString("ContentParseRule");
        if (ModelCmsCatchParseContent.ContentParseRule == "")
        {
            nsb.AddErr("请输入内容解析规则。");
        }
        ModelCmsCatchParseContent.ContentPageParseRule = PageRequest.GetString("ContentPageParseRule");
        //特殊信息
        ModelCmsCatchParseContent.UpdateTime = System.DateTime.Now;
        ModelCmsCatchParseContent.UpdateUserID = 10000;
        ModelCmsCatchParseContent.UpdateUserName = "admin";
        nsb.ChkErr();
        int RetFlag = BLLCmsCatchParseContent.Update(ModelCmsCatchParseContent);
        if (RetFlag < 0)
        {
            nsb.AddErr("对不起！已存在相同的内容模型。");
            nsb.ChkErr();
        }
        else
        {
            //ManageHelper.AddLog("system_catchparesmanageupdate", "修改了采集解析：" + ModelCmsCatchParseContent.ParseContentName);
            Response.Redirect("CatchSingleParseList.aspx?sucmsg=修改成功。");
        }

    }









    #region 集解析内容调整——ajax

    #region 删除内容补充
    private void RegDel(int ID)
    {
        KeRui.Model.CmsCatchParseRegexContent modelRegx = new CmsCatchParseRegexContent();
        modelRegx = BLLRegexContent.GetModel(ID);
        if (modelRegx == null)
        {
            Response.Write("信息不存在。"); Response.End();
        }
        else
        {
            BLLRegexContent.Delete(ID);
            Response.Write(BoundData(modelRegx.ParseContentID)); Response.End();
        }
    }
    #endregion


    #region 组建添加页面
    private void RegAdd()
    {
        ModelCmsCatchParseContent = BLLCmsCatchParseContent.GetModel(ParseID);
        if (ModelCmsCatchParseContent == null)
        {
            Response.Write("ddd" + ParseID); Response.End();
        }
        StringBuilder RetMsg = new StringBuilder();
        RetMsg.Append("<div class=\"columntitle\">解析内容</div><form id=\"myfrm\">" +
        "<table width=\"90%\">" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"\" onclick=\"helpInf(this)\" /> 字段名称：</td>" +
                "<td align=\"left\">" +
                    "<select name=\"FieldName\" id=\"FieldName\">" +
                    GetSelect(ModelCmsCatchParseContent.ContentParseRule, "") +
                    "</select>" +
                "</td>" +
            "</tr> " +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"匹配正则表达式模式\" onclick=\"helpInf(this)\" /> 匹配正则表达式模式：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Pattern\" ID=\"Pattern\" valid=\"required\" errmsg=\"请输入匹配正则表达式模式\"style=\"width:300px;height:60px;\" class=\"colorblur\"  ></textarea>" +
                "</td>" +
            "</tr>" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"替换字符串\" onclick=\"helpInf(this)\" /> 替换字符串：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Replacement\" ID=\"Replacement\" style=\"width:300px;height:60px;\" class=\"colorblur\"  ></textarea>" +
                "</td>" +
            "</tr>  " +
             "<tr>" +
                "<td colspan=\"2\" align=\"center\" class=\"tdbutton\">" +
                "<input type=\"button\" name=\"btnAdd\" value=\"保 存\" onclick=\"if(validator($$('myfrm'))){RegContentAddSave(" + ModelCmsCatchParseContent.ParseContentID + ")}\"/> " +
                "<input type=\"button\" name=\"Cancel\" value=\"关 闭\" onclick=\"javascript:document.getElementById('RegDiv').style.display='none';\"/>" +
                "<input type=\"hidden\" value=\"" + ModelCmsCatchParseContent.ParseContentID + "\" name=\"pcid\" ID=\"pcid\" />" +
                "</td>" +
            "</tr>" +
        "</table></from>");
        Response.Write(RetMsg.ToString());
        Response.End();
    }
    #endregion

    #region 添加时保存
    private void RegAddSave()
    {
        string RetStr = "";
        ModelCmsCatchParseRegexContent.ParseContentID = ParseID;
        if (ModelCmsCatchParseRegexContent.ParseContentID == -1)
        {
            Response.Write("1"); Response.End();
        }
        ModelCmsCatchParseRegexContent.FieldName = PageRequest.GetString("FieldName");
        if (ModelCmsCatchParseRegexContent.FieldName == "")
        {
            Response.Write("2"); Response.End();
        }
        ModelCmsCatchParseRegexContent.Pattern = PageRequest.GetString("Pattern");
        if (ModelCmsCatchParseRegexContent.Pattern == "")
        {
            Response.Write("3"); Response.End();
        }
        ModelCmsCatchParseRegexContent.Replacement = PageRequest.GetString("Replacement");
        ModelCmsCatchParseRegexContent.CreateTime = System.DateTime.Now;

        BLLRegexContent.Add(ModelCmsCatchParseRegexContent);

        RetStr = BoundData(ModelCmsCatchParseRegexContent.ParseContentID);

        Response.Write(RetStr); Response.End();
    }
    #endregion

    #region 组建编辑页面
    private void RegEdit()
    {
        StringBuilder RetMsg = new StringBuilder();
        int ID = PageRequest.GetInt("ID", -1);
        ModelCmsCatchParseRegexContent = BLLRegexContent.GetModel(ID);
        if (ModelCmsCatchParseRegexContent == null)
        {
            Response.Write("error"); Response.End();
        }
        ModelCmsCatchParseContent = BLLCmsCatchParseContent.GetModel(ModelCmsCatchParseRegexContent.ParseContentID);
        if (ModelCmsCatchParseContent == null)
        {
            Response.Write("error" + ModelCmsCatchParseRegexContent.ParseContentID); Response.End();
        }
        RetMsg.Append("<div class=\"columntitle\">解析内容</div><form id=\"myfrm\">" +
        "<table width=\"90%\">" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"\" onclick=\"helpInf(this)\" /> 字段名称：</td>" +
                "<td align=\"left\">" +
                    "<select name=\"FieldName\" id=\"FieldName\">" +
                    GetSelect(ModelCmsCatchParseContent.ContentParseRule, ModelCmsCatchParseRegexContent.FieldName) +
                    "</select>" +
                "</td>" +
            "</tr> " +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"匹配正则表达式模式\" onclick=\"helpInf(this)\" /> 匹配正则表达式模式：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Pattern\" ID=\"Pattern\" valid=\"required\" errmsg=\"请输入匹配正则表达式模式\"style=\"width:300px;height:60px;\" class=\"colorblur\"  >" + ModelCmsCatchParseRegexContent.Pattern + "</textarea>" +
                "</td>" +
            "</tr>" +
            "<tr>" +
                "<td align=\"left\" width=\"140\">" +
                    "<img src=\"../pic/icons/help.gif\" alt=\"替换字符串\" onclick=\"helpInf(this)\" /> 替换字符串：</td>" +
                "<td align=\"left\">" +
                    "<textarea  name=\"Replacement\" ID=\"Replacement\" style=\"width:300px;height:60px;\" class=\"colorblur\"  >" + ModelCmsCatchParseRegexContent.Replacement + "</textarea>" +
                "</td>" +
            "</tr>  " +
             "<tr>" +
                "<td colspan=\"2\" align=\"center\" class=\"tdbutton\">" +
                "<input type=\"button\" name=\"btnAdd\" value=\"保 存\" onclick=\"if(validator($$('myfrm'))){RegContentEditSave(" + ModelCmsCatchParseRegexContent.ID + ")}\"/> " +
                "<input type=\"button\" name=\"Cancel\" value=\"关 闭\" onclick=\"javascript:document.getElementById('RegDiv').style.display='none';\"/>" +
                "<input type=\"hidden\" value=\"" + ModelCmsCatchParseRegexContent.ParseContentID + "\" name=\"pcid\" ID=\"pcid\" />" +
                "</td>" +
            "</tr>" +
        "</table></from>");
        Response.Write(RetMsg.ToString());
        Response.End();
    }
    #endregion

    #region 编辑时保存
    private void RegEditSave()
    {
        int ID = PageRequest.GetInt("ID", -1);

        ModelCmsCatchParseRegexContent = BLLRegexContent.GetModel(ID);
        if (ModelCmsCatchParseRegexContent == null)
        {
            Response.Write("error"); Response.End();
        }
        string RetStr = "";
        ModelCmsCatchParseRegexContent.FieldName = PageRequest.GetString("FieldName");
        if (ModelCmsCatchParseRegexContent.FieldName == "")
        {
            Response.Write("2"); Response.End();
        }
        ModelCmsCatchParseRegexContent.Pattern = PageRequest.GetString("Pattern");
        if (ModelCmsCatchParseRegexContent.Pattern == "")
        {
            Response.Write("3"); Response.End();
        }
        ModelCmsCatchParseRegexContent.Replacement = PageRequest.GetString("Replacement");
        //if (modelRegx.Replacement == "")
        //{
        //    Response.Write("4"); Response.End();
        //}
        ModelCmsCatchParseRegexContent.CreateTime = System.DateTime.Now;
        BLLRegexContent.Update(ModelCmsCatchParseRegexContent);
        RetStr = BoundData(ModelCmsCatchParseRegexContent.ParseContentID);
        Response.Write(RetStr); Response.End();
    }
    #endregion

    #region 删除
    private void RegDel()
    {
        int ID = PageRequest.GetInt("ID", -1);
        ModelCmsCatchParseRegexContent = BLLRegexContent.GetModel(ID);
        if (ModelCmsCatchParseRegexContent == null)
        {
            Response.Write("信息不存在。"); Response.End();
        }
        else
        {
            BLLRegexContent.Delete(ID);
            Response.Write(BoundData(ModelCmsCatchParseRegexContent.ParseContentID));
            Response.End();
        }
    }
    #endregion

    #region 绑定数据
    protected string BoundData(int ParseID)
    {
        StringBuilder RetData = new StringBuilder();
        RetData.Append("<table cellspacing=\"0\" cellpadding=\"0\" rules=\"all\"  border=\"0\" id=\"MyDataGrid\" style=\"border-width: 0px; width: 100%; border-collapse: collapse;\">");
        RetData.Append("<tr class=\"summary-title\" style=\"height: 25px;\">" +
                    "<td>&nbsp;字段名称</td>" +
                    "<td>匹配正则表达式模式</td>" +
                    "<td>替换字符串</td>" +
                    "<td>创建时间</td>" +
                    "<td>操作</td>" +
                "</tr>");

        listRegex = BLLRegexContent.GetList(ParseID);
        foreach (KeRui.Model.CmsCatchParseRegexContent model in listRegex)
        {
            RetData.Append("<tr class=\"tdbg\" onmouseover=\"this.className='tdbg-dark';\" onmouseout=\"this.className='tdbg';\" style=\"height:25px;\">" +
                        "<td>&nbsp;" + model.FieldName + "</td>" +
                        "<td>" + HttpUtility.HtmlEncode(model.Pattern) + "</td>" +
                        "<td>" + model.Replacement + "</td>" +
                        "<td>" + model.CreateTime + "</td>" +
                        "<td><a href=\"javascript:RegContentEditPage(" + model.ID + ")\">修改<a> | <a href=\"javascript:if(confirm('确定要删除。')){RegContentDel(" + model.ID + ")}\">删除</a></td>" +
                    "</tr>");
        }
        RetData.Append("</table>");
        return RetData.ToString();
    }

    private string GetSelect(string strRegx, string strTep)
    {
        string retStr = "";
        KeRui.BLL.CmsCatchParseRegex bllRegx = new KeRui.BLL.CmsCatchParseRegex();
        string[] arr = bllRegx.GetRegexFiledArray(strRegx);
        for (int i = 0; i < arr.Length; i++)
        {
            string strSel = "";
            if (strTep == arr[i])
            {
                strSel = "selected";
            }
            retStr += "<option value=" + arr[i] + " " + strSel + ">" + arr[i] + "</option>";
        }
        return retStr;
    }
    #endregion
    #endregion
}
